﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace _180FrameWork.Common
{
    public class JsonCustomBoolConvert : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            if (objectType == typeof(bool))
            {
                return true;
            }
            else if (objectType == typeof(int))
            {
                return true;
            }
            else if (objectType == typeof(double))
            {
                return true;
            }
            else if (objectType == typeof(decimal))
            {
                return true;
            }
            else if (objectType == typeof(float))
            {
                return true;
            }
            else if (objectType == typeof(string))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            try
            {
                return Convert.ToBoolean(reader.Value);
            }
            catch
            {
                return false;
            }
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            writer.WriteValue((bool)value);
        }
    }
}
